//
//  LXCommSearchController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/30.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXCommSearchController.h"
#import "LXInsuranceTableViewCell.h"
#import "LXInsuranceApplyViewController.h"
#import "LXMainTainApplyViewController.h"
#import "LXYearCheckApplyViewController.h"
#import "LXRepairApplyViewController.h"


#import "LXMainTainModel.h"
#import "LXInsuranceModel.h"
#import "LXYearCheckModel.h"
#import "LXRepairModel.h"

@interface LXCommSearchController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSInteger pageIndex;
@property (nonatomic) LXTopSearchView *searchView;
@property (nonatomic) NSString *searchText;

@end

@implementation LXCommSearchController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    

    [self layoutCustomNav:self.view title:@"搜索"];
    
    if (kUserDefaultForBool(kRefreshStr)) {
        [self.tableView.mj_header beginRefreshing];
    }
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    LXTopSearchView *searchView = [[LXTopSearchView alloc] initWithFrame:CGRectMake(0,LL_StatusBarAndNavigationBarHeight, kScreenWidth, 70)];
    searchView.textField.placeholder = @"输入车牌号查询";
    [self.view addSubview:searchView];
    self.searchView = searchView;
    // 点击
    [searchView setClickBtnBlock:^(NSString * _Nonnull text) {
        [self.view endEditing:YES];
        
        if (!StringIsEmpty(text)) {
            self.searchText = text;
            [self.tableView.mj_header beginRefreshing];
        }
        else {
            
            showAlert(@"请输入关键字搜索");
            
            [self.data removeAllObjects];
            [self.tableView reloadData];
            [self isHaveEmptyData];
        }
        
        
    }];
    
    
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXInsuranceTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXInsuranceTableViewCell class])];
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self loadData];
    }];

    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
         self.pageIndex++;
        [self loadData];
    }];
    
    [self isHaveEmptyData];
    
}
- (void)loadData{
        
    
    NSString *url = [DOMAIN_URL stringByAppendingFormat:self.url,@(self.pageIndex),@(10)];
    url = [url stringByAppendingString:@"&isRemind=1"];
   
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (!StringIsEmpty(self.searchText)) {
        url = [url stringByAppendingFormat:@"&carNum=%@",self.searchText];
    }
    [MSNetworkManager getRevertUrl:url params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        
        if ([self.tableView.mj_header isRefreshing]||self.pageIndex==1) {
            [self.data removeAllObjects];
            [self.tableView.mj_footer resetNoMoreData];
        }
        NSInteger totalPage = [resonseObj[@"total"] integerValue];
            
        NSArray *records = resonseObj[@"records"];
        
        [self.data addObjectsFromArray:records];
        
        
        if (self.data.count>=totalPage) {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
        else {
            self.tableView.mj_footer.state = MJRefreshStateIdle;
        }
        
            
        [self.tableView reloadData];
            
        [self isHaveEmptyData];
            
       
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];

}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    LXInsuranceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXInsuranceTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setClipsToBounds:YES];
    
    if (self.type == LXInsurance_TYPE) {
        LXInsuranceModel *model = [LXInsuranceModel mj_objectWithKeyValues:self.data[indexPath.row]];
        cell.insuranceModel = model;
    }
    else if(self.type == LXRepair_TYPE){
       
        LXRepairModel *model = [LXRepairModel mj_objectWithKeyValues:self.data[indexPath.row]];
        cell.repairModel = model;
       
    }
    else if(self.type == LXYearCheck_TYPE){
       
        LXYearCheckModel *model = [LXYearCheckModel mj_objectWithKeyValues:self.data[indexPath.row]];
        cell.yearCheckModel = model;
        
    }
    else if(self.type == LXMainTain_TYPE){
       
        LXMainTainModel *model = [LXMainTainModel mj_objectWithKeyValues:self.data[indexPath.row]];
        cell.mainTainModel = model;
        
    }
    return cell;
   
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (self.type == LXInsurance_TYPE) {
        LXInsuranceApplyViewController *vc = [[LXInsuranceApplyViewController alloc] init];
        vc.type = 2;
        vc.insuranceModel = [LXInsuranceModel mj_objectWithKeyValues:self.data[indexPath.row]];
        [self pushToNextController:vc];
    }
    else  if (self.type == LXMainTain_TYPE) {
        LXMainTainApplyViewController *vc = [[LXMainTainApplyViewController alloc] init];
        vc.type = 2;
        vc.mainTainModel = [LXMainTainModel mj_objectWithKeyValues:self.data[indexPath.row]];
        [self pushToNextController:vc];
    }
    else  if (self.type == LXYearCheck_TYPE) {
        LXYearCheckApplyViewController *vc = [[LXYearCheckApplyViewController alloc] init];
        vc.type = 2;
        vc.yearCheckModel = [LXYearCheckModel mj_objectWithKeyValues:self.data[indexPath.row]];
        [self pushToNextController:vc];
    }
    else  if (self.type == LXRepair_TYPE) {
        LXRepairApplyViewController *vc = [[LXRepairApplyViewController alloc] init];
        vc.type = 2;
        vc.repairModel = [LXRepairModel mj_objectWithKeyValues:self.data[indexPath.row]];
        [self pushToNextController:vc];
    }
    
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, MaxY(self.searchView), kScreenWidth, kScreenHeight - MaxY(self.searchView)) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 108;
        _tableView.backgroundColor = ColorWithF5F5F5;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}


- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}
- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}
@end
